package com.ymatou.iapi.sellerproduct.testcase;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.BatchSetProductOnSaleCall;
import com.ymatou.iapi.sellerproduct.service.SynchProductToMongoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ProductBasicInfoWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;

@TestSuite
public class Ts_synchProductToMongo{
	private static SynchProductToMongoCall synchProductToMongoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量上架商品");
	}

	@Before
	public void caseUp() {
		synchProductToMongoCall=new SynchProductToMongoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(synchProductToMongoCall.getOpurl());
	}
	
	/**
	 * 获取配置库中的数据
	 * @return
	 */
	private Map getUser() {
		HashMap<String, Comparable> user = new HashMap<String, Comparable>();
		user.put("sellerId", Integer.valueOf(EnvSetup.getData("productsellerid")));
		user.put("sellerName", EnvSetup.getData("productsellername"));
		user.put("sellerId_Child", Integer.valueOf(EnvSetup.getData("productselleridChild")));
		user.put("sellerName_Child", EnvSetup.getData("productsellernameChild"));
		user.put("productActivityStartId", EnvSetup.getData("productActivityStartId"));
		user.put("activityLoginName", EnvSetup.getData("activityLoginName"));
		user.put("activityLoginPswd", EnvSetup.getData("activityLoginPswd"));
		return user;		
	}	

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_001() {
		Logger.start(true,"上架多个商品");
		try {
			//TODO 补全case代码
			Map<String, Object> user = getUser();
			int sellerId = (Integer) user.get("sellerId");
			String sellerName = (String) user.get("sellerName") ;
			
			//获取当前时间&结束时间
			Date startTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-18));
			Date endTime = (new SimpleDateFormat("yyyy-MM-dd")).parse(YMTDateUtil.getBeforeOrNextDay(-11));
			
			//发布一个立即上架商品
			String productID1 = addNewProductOnSale(sellerId, sellerName);
					
			//sql更新上架时间
			YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
			ymtProductsIWapper.updateValidtimeByProductId(startTime,endTime,productID1);
			
			//Call
			List<String> list=new ArrayList<>();
			list.add(productID1);
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("ProductIds", new JSONArray(list));				
			JSONObject result = synchProductToMongoCall.callAndGetReturnData(maplist);
			
			
			//断言返回报文
			synchProductToMongoreturncode(result);
			//SQL        
			List<Map> productIDsqllist1 = ymtProductsIWapper.selectProductByProductIdforMap(productID1);
			Date svalidStartbysql1 = (Date) productIDsqllist1.get(0).get("validStart");			
			Date svalidEndbysql1 = (Date) productIDsqllist1.get(0).get("validEnd");			
			//断言
			Logger.verifyEquals(startTime, svalidStartbysql1, "商品1开始销售时间");
			Logger.verifyEquals(endTime, svalidEndbysql1, "商品1结束销售时间");
			
			//Mango
			ProductBasicInfoWapper productBasicInfoWapper = new ProductBasicInfoWapper();
			Map<String, Object> productBasicInfo1 = productBasicInfoWapper.getProductId(productID1); 		
			Date validStartbyMango1 = (Date) productBasicInfo1.get("ValidStart");	
			Date ValidEndbyMango1 = (Date) productBasicInfo1.get("ValidEnd");				
			//断言
			Logger.verifyEquals(startTime, validStartbyMango1, "商品1开始销售时间");
			Logger.verifyEquals(endTime, ValidEndbyMango1, "商品1结束销售时间");

			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	
	
	
	
	
	
	

	
	
	/**
	 * 接口返回信息公用信息部分断言（正向Case场景）
	 * @param dataStr 传入报文信息
	 * @throws Exception 错误信息
	 */
	public void synchProductToMongoreturncode(JSONObject dataStr) throws Exception {
	    try {
			//返回报文断言
			String code =  dataStr.getString("Code");
			Logger.verifyIsContains(200, code, "\"Code\":200");
			String bCode =  dataStr.getString("BCode");
			Logger.verifyIsContains(0, bCode, "\"BCode\":0");
			String msg =  dataStr.getString("Msg");
			Logger.verifyIsContains("", msg, "Msg操作成功");
			String trace =  dataStr.getString("Trace");
			Logger.verifyIsNull(trace, "Trace等于空");			

	    }catch (Exception e) {
	        e.printStackTrace();
	    }
	}

	/**
	 * 发布一个立即上架商品
	 * @return 商品id
	 * @throws Exception
	 */
	public String addNewProductOnSale(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject addNewProductOnSaleObject = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProductOnSaleObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID =  addNewProductOnSaleObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			return productID;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}
	
	/**
	 * 发布一个未上架商品
	 * @return 商品id
	 * @throws Exception
	 */
	public String addNewProductNotOnSale(int sellerId, String sellerName) throws Exception {
	    try {	    	
			//发布商品
			JSONObject addNewProductOnSaleObject = SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null);
			Logger.verifyEquals(addNewProductOnSaleObject.getInt("Code"), 200, "响应报文-返回码");
			//获取返回报文的ProductID值
			String productID =  addNewProductOnSaleObject.getJSONObject("Data").getString("ProductID");
			Logger.verifyNotNull(productID, "响应报文-商品ID");
			return productID;
	    }catch (Exception e) {  
	        e.printStackTrace();	        
	    }  
	    return null;
	}

	
}